<?php

//This file will handle the sorting function in the assignment
//Use an HTML form to select how you want it to sort
//It sorts and then reoders and displays the selected
//sort category using an HTML table and a temporary array.
//Saxon Dakin, 7392176

include 'menu.php';

//HTML form to select how you wish to sort the data.
?>
  <html><body>
  <h4>Sort the list of Favourites</h4>
  <form action="sort.php" method="post"> 
                         
  <!-- Drop down box for Sort Category-->
  Sort By: <select name="sort"> 
  <option>URL</option>
  <option>Category</option>
  <option>Date</option>
  <option>Counter</option>
  </select>
  <br>
  <br>
  <input type="submit" name="submit" />
  </form>
  </body></html>

<?php
//If loop to display the default table prior to submit.
if(!isset($_POST['submit']))
{
$bigarray = file("favourites.txt"); 

echo "<table border='1'><tr><th>URL</th><th>Category</th><th>Date</th><th>Counter</th></tr>"; 
foreach ($bigarray as $value) 
{ 
    $smallarray = explode("|", $value); 
    echo "<tr>"; 
    echo "<td>$smallarray[0]</td>";
    echo "<td>$smallarray[1]</td>";
    echo "<td>$smallarray[2]</td>";
    echo "<td>$smallarray[3]</td>"; 
    echo "</tr>"; 
}     
echo '</table>';
}

else
{
  //After submit the $sortorder is obtained via $_POST. 
  $sortorder = $_POST['sort'];
  $heading = ""; 
    
  // Load text file into array 
  $bigarray = file("favourites.txt"); 
  $temparray = array(); 
  foreach ($bigarray as $value) 
  { 
      //Break each row/record into elements of a small array 
      $smallarray = explode("|", $value); 
      
      //Assign elements of the smallarray to relevant vars 
      $url = $smallarray[0]; 
      $category = $smallarray[1]; 
      $date = $smallarray[2]; 
      $count = $smallarray[3]; 
      
      //Arrange elements differently, so that sort column comes first 
      //Each element of temparray is itself an array 
      if ($sortorder == "URL") 
      { 
        $temparray[] = array($url,$category,$date,$count);
        $heading = "<table border='1'><tr><th>URL</th><th>Category</th><th>Date</th><th>Counter</th></tr>";
      } 
      elseif ($sortorder == "Category") 
      { 
        $temparray[] = array($category,$url,$date,$count); 
        $heading = "<table border='1'><tr><th>Category</th><th>URL</th><th>Date</th><th>Counter</th></tr>";
      }
      elseif ($sortorder == "Date") 
      { 
        $temparray[] = array($date,$url,$category,$count);
        $heading = "<table border='1'><tr><th>Date</th><th>URL</th><th>Category</th><th>Counter</th></tr>"; 
      }
      elseif ($sortorder == "Counter") 
      { 
        $temparray[] = array($count,$url,$category,$date); 
        $heading = "<table border='1'><tr><th>Counter</th><th>URL</th><th>Category</th><th>Date</th></tr>";
      }
      else
      { 
          $temparray[] = array($url,$category,$date,$count);
          $heading = "<table border='1'><tr><th>URL</th><th>Category</th><th>Date</th><th>Counter</th></tr>"; 
      } 
   }
  //After rearranging the $temparray is sorted and displayed.  
  sort($temparray); 
  echo $heading; 
  foreach ($temparray as $value) 
  { 
      echo "<tr>"; 
      echo "<td>$value[0]</td>";
      echo "<td>$value[1]</td>";
      echo "<td>$value[2]</td>";
      echo "<td>$value[3]</td>"; 
      echo "</tr>"; 
  }     
  echo '</table>'; 
}

?>